# Maintainer: Martell Malone < martell malone at g mail dot com >
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=go
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.21.0
pkgrel=2
pkgdesc="Go Lang (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=('spdx:BSD-3-Clause')
url="https://www.golang.org/"
depends=()
makedepends=("${MINGW_PACKAGE_PREFIX}-${_realname}"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=('!strip')
source=("https://go.dev/dl/go${pkgver}.src.tar.gz"{,.asc})
sha256sums=('818d46ede85682dd551ad378ef37a4d247006f12ec59b5b755601d2ce114369a'
            'SKIP')
validpgpkeys=('EB4C1BFD4F042F6DDDCCEC917721F63BD38B4796')
noextract=(go${pkgver}.src.tar.gz)

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}
# =========================================== #

prepare() {
  tar -xzf ${srcdir}/go${pkgver}.src.tar.gz -C ${srcdir} || true
}

build() {
  cd "${srcdir}"/${_realname}/src
  export GOROOT_BOOTSTRAP=${MINGW_PREFIX}/lib/go
  export GOROOT_FINAL=${MINGW_PREFIX}/lib/go
  export GO_CFLAGS="-D__USE_MINGW_ANSI_STDIO=1"
  export CFLAGS="-D__USE_MINGW_ANSI_STDIO=1"
  export GO_BUILD_VERBOSE=1

  cmd //c make.bat
}

check() {
  cd "${_realname}"

  test_text="Hello MSYS2!"

  rm -f /tmp/test_main.go
  echo "package main"                    > /tmp/test_main.go
  echo "import \"fmt\""                 >> /tmp/test_main.go
  echo "func main() {"                  >> /tmp/test_main.go
  echo "fmt.Println(\"${test_text}\")"  >> /tmp/test_main.go
  echo "}"                              >> /tmp/test_main.go

  ./bin/gofmt.exe -w /tmp/test_main.go
  ./bin/go.exe build -o /tmp/test_result.exe /tmp/test_main.go
  output=$(/tmp/test_result.exe)

  if [[ "$output" != "$test_text" ]]; then
    echo "Output \"${output}\" does not match test-text \"${test_text}\""
    exit 1
  fi

  rm -f /tmp/test_main.go /tmp/test_result.exe
}

package() {
  cd "${_realname}"

  export GOROOT="${srcdir}/${_realname}"
  export GOBIN="${GOROOT}/bin"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/"{bin,lib/go,lib/go/doc,lib/go/src,lib/go/site/src,share/licenses/go,share/go}

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/licenses/go"
  install -Dm644 "${srcdir}"/${_realname}/LICENSE \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/go/LICENSE"

  cp -rf bin "${pkgdir}${MINGW_PREFIX}"
  cp -rf bin pkg src lib misc api test "${pkgdir}${MINGW_PREFIX}/lib/go"
  cp -r doc/* "${pkgdir}${MINGW_PREFIX}/lib/go/doc/"

  install -Dm644 VERSION "${pkgdir}${MINGW_PREFIX}/lib/go/VERSION"

  rm -rf "${pkgdir}${MINGW_PREFIX}/lib/go/pkg/bootstrap" "${pkgdir}${MINGW_PREFIX}/lib/go/pkg/tool/*/api"

  # TODO: Figure out if really needed
  rm -rf "${pkgdir}${MINGW_PREFIX}"/lib/go/pkg/obj/go-build/*

  # https://github.com/golang/go/issues/57179
  install -Dm644 go.env "${pkgdir}${MINGW_PREFIX}/lib/go/go.env"

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/go/LICENSE"
 
  # install profile script 
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/etc/profile.d
  echo "export GOROOT=${MINGW_PREFIX}/lib/go" > "${pkgdir}${MINGW_PREFIX}"/etc/profile.d/go.sh
  cp "${pkgdir}${MINGW_PREFIX}"/etc/profile.d/go.{sh,zsh}
}
